﻿using System;
using Xoohoo.Models;
using Xoohoo.Extensions;
using Xoohoo.Services;
using Xoohoo.Utilities.Cryptography;

namespace Xoohoo.Modules.Admin.Extensions
{
    public static class IUserServiceExtensions
    {
        public static User GetNormalUser(this IUserService userService, string username, string password)
        {
            User user = userService.GetItem(username,UserStatus.Normal);

            if (user != null)
            {
                string currentPassword = user.Password;

                if (!currentPassword.IsNullOrWhiteSpace())
                {
                    string[] splitData = currentPassword.Split('|');

                    if (splitData.Length != 2) throw new InvalidOperationException("Password and PasswordSalt could not be read from module data");

                    string userPasswordSalt = splitData[0];
                    string userPassword = splitData[1];
                    if (userPassword == SHA256.Encrypt(password,userPasswordSalt))
                        return user;
                }
            }

            return null;
        }
    }
}
